package com.controller;

import com.comment.CommentResult;
import com.domain.entity.User;
import com.domain.req.*;
import com.github.pagehelper.PageInfo;
import com.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@CrossOrigin  //允许跨域
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;


    /**
     * 通过账号密码登录
     *
     * @return 登录成功的token
     */
    @PostMapping("/login")
    public CommentResult<String> login(@RequestBody LoginReq loginReq) {
        String token = userService.login(loginReq);
        return CommentResult.success(token);
    }

    /**
     * 注册普通用户
     *
     * @param registerReq 注册请求
     */
    @PostMapping("/register")
    public CommentResult<Void> register(@RequestBody RegisterReq registerReq) {
        userService.register(registerReq);
        return CommentResult.success();
    }


    /**
     * 根据用户信息和分页请求分页查询用户信息
     * @param pageReq 分页请求
     * @param user 用户信息
     */
    @GetMapping("/pageList")
    public CommentResult<PageInfo<User>> pageList(PageReq pageReq, User user) {
        PageInfo<User> userPageInfo = userService.pageList(pageReq, user);
        return CommentResult.success(userPageInfo);
    }

    /**
     * 获取当前用户信息
     */
    @GetMapping("/info")
    public CommentResult<User> info() {
        User user = userService.info();
        return CommentResult.success(user);
    }

    /**
     * 根据用户id查询用户信息
     */
    @GetMapping("/getUserById/{id}")
    public CommentResult<User> getUserById(@PathVariable("id") Integer id) {
        User user = userService.getUserById(id);
        return CommentResult.success(user);
    }

    /**
     * 根据用户id删除用户信息
     * @param id 用户id
     */
    @DeleteMapping("/deleteById/{id}")
    public CommentResult<Void> deleteById(@PathVariable("id") Integer id) {
        userService.deleteById(id);
        return CommentResult.success();
    }

    /**
     * 根据id修改用户信息
     * @param userUpdateReq 修改用户信息的请求
     */
    @PutMapping("/updateById")
    public CommentResult<Void> updateById(@RequestBody UserUpdateReq userUpdateReq) {
        userService.updateById(userUpdateReq);
        return CommentResult.success();
    }

    /**
     * 修改密码
     */
    @PutMapping("/changePassword")
    public CommentResult<Void> changePassword(ChangePasswordReq changePasswordReq) {
        userService.changePassword(changePasswordReq);
        return CommentResult.success();
    }

    /**
     * 充值
     */
    @PutMapping("/recharge")
    public CommentResult<Void> recharge(@RequestBody RechargeReq registerReq) {
        userService.recharge(registerReq);
        return CommentResult.success();
    }

}
